#!/usr/bin/env python
#Boa:PyApp:main

import consultasBD
import utiles
import re

modules ={}

def procesarStock(sucursal,estacion,f):
    r1 = "SELECT p3.ptc,p3.codsussan,p3.estacion,p3.anio,p4.stock "
    r2 = "FROM "
    r3 = " (SELECT p2.id as ptc,p2.codsussan,p1.estacion,p1.anio "
    r4 = "  FROM "
    r5 = "   (SELECT codsussan,estacion,anio FROM productos "
    r6 = "    WHERE estacion='"+estacion+"' AND anio<>'2012') AS p1, "
    r7 = "   producto_talle_color AS p2 "
    r8 = "  WHERE p1.codsussan=p2.codsussan) AS p3, "
    r9 = " stock AS p4 "
    r0 = "WHERE p3.ptc=p4.ptc AND p4.stock<>0 AND sucursal='"+sucursal+"' "
    sql = r1+r2+r3+r4+r5+r6+r7+r8+r9+r0
    resultados = consultasBD.ordenCrudaDict(sql)
    total = 0
    for item in resultados:
        r1 = "UPDATE stock SET stock='0' "
        r2 = "WHERE ptc='%s' AND sucursal='%s' ;\n" % (item["ptc"],sucursal)
        sql = r1+r2
        f.write(sql)
        total+= 1
    print "Total sucursal",sucursal,"->",total,"Estacion:",estacion

def main():
    f=open("ponerencero.txt","w")
    procesarStock("2","Invierno",f)
    procesarStock("3","Invierno",f)
    procesarStock("4","Invierno",f)
    procesarStock("2","Otono",f)
    procesarStock("3","Otono",f)
    procesarStock("4","Otono",f)
    procesarStock("2","Lince-Oton",f)
    procesarStock("3","Lince-Oton",f)
    procesarStock("4","Lince-Oton",f)
    procesarStock("2","Lince-Invi",f)
    procesarStock("3","Lince-Invi",f)
    procesarStock("4","Lince-Invi",f)
    f.close()
    print "Comando a emplear:"
    print "mysql -uerpsussan -p<password> erpsussan < c:\erpsussan\ponerencero.txt"

if __name__ == '__main__':
    main()
